package org.xqp.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xqp.basic.exception.MyException;
import org.xqp.basic.service.impl.BaseServiceImpl;
import org.xqp.org.domain.Employee;
import org.xqp.org.domain.Shop;
import org.xqp.org.mapper.EmployeeMapper;
import org.xqp.org.mapper.ShopMapper;
import org.xqp.org.service.IEmployeeService;
import org.xqp.org.service.IShopService;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void shopRegister(Shop shop) {
        //验证入驻的时候输入新密码与确认密码是否一致
        Employee admin = shop.getAdmin();
        if(!admin.getPassword().equals(shop.getComfirmPassword())){
            throw new MyException("两次密码不一致");
        }
        // 用户名是否重复  （这里的用户名既是username也是邮箱，还有可能是电话号码）
        Employee employee = employeeMapper.findByUsername(admin.getUsername());
        if(employee!=null){
            throw new MyException("用户已存在，请重新输入");
        }
        //前台传过来的数据中先添加employee对象 然后返回该对象的自增主键id
        employeeMapper.add(admin); // 添加employee对象

        //将employee对象的自增主键id设置到shop中再添加shop，也要返回shop的自增主键id
        Long id = admin.getId(); // 获取到employee对象的自增id
        shop.setAdmin_id(id);   // 将id存入 shop中的admin_id属性中
        shopMapper.add(shop);   // 添加该shop对象
        // 将shop的自增主键id在放入刚才添加的employee对象的属性shop_id并更新employee
        admin.setShop_id(shop.getId()); // 将shop对象的自增id放入employee对象中
        employeeMapper.update(admin); // 更新
    }
}